package cn.iwit;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.core.env.Environment;

@Configuration
public class AppConfig {
	@Autowired
	private Environment env;

	@Bean
	public DefaultKaptcha defaultKaptcha() {
		DefaultKaptcha kaptcha = new DefaultKaptcha();
		Properties prop = new Properties();
		prop.setProperty("kaptcha.border", "no");
		prop.setProperty("kaptcha.image.width", "90");
		prop.setProperty("kaptcha.image.height", "35");
		prop.setProperty("kaptcha.textproducer.font.size", "28");
		prop.setProperty("kaptcha.textproducer.font.color", "black");
		prop.setProperty("kaptcha.textproducer.char.string", "1234567890");
		prop.setProperty("kaptcha.textproducer.char.length", "4");
		prop.setProperty("kaptcha.textproducer.char.space", "5");
		prop.setProperty("kaptcha.noise.impl",
				"com.google.code.kaptcha.impl.NoNoise");
		prop.setProperty("kaptcha.noise.color", "150,150,150");
		prop.setProperty("kaptcha.background.clear.from", "white");
		prop.setProperty("kaptcha.background.clear.to", "white");
		prop.setProperty("kaptcha.obscurificator.impl",
				"com.google.code.kaptcha.impl.WaterRipple");
		Config config = new Config(prop);
		kaptcha.setConfig(config);
		return kaptcha;
	}

	@Bean
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl(env.getProperty("spring.datasource.url"));
		dataSource.setUsername(env.getProperty("spring.datasource.username"));// 用户名
		dataSource.setPassword(env.getProperty("spring.datasource.password"));// 密码
		dataSource.setInitialSize(2);
		dataSource.setMaxActive(20);
		dataSource.setMinIdle(0);
		dataSource.setMaxWait(60000);
		dataSource.setValidationQuery("SELECT 1");
		dataSource.setTestOnBorrow(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setPoolPreparedStatements(false);
		return dataSource;
	}
}
